namespace Sustainalytics.Portfolios.Service.Utility
{
    using System;
    using System.Collections.Generic;
    using System.Net;

    using Sustainalytics.DataLayer.Exceptions;
    using Sustainalytics.Portfolios.BL.Exceptions;
    using Sustainalytics.Portfolios.Service.ErrorCodes;
    using Sustainalytics.ScreeningTool.BL.Exceptions;
    using Sustainalytics.Utils.WebApi.ErrorCodes;

    public static class ErrorMessageFactory
    {
        private static readonly Dictionary<Type, Tuple<HttpStatusCode, object>> HandledExceptions =
            new Dictionary<Type, Tuple<HttpStatusCode, object>>
                {
                    { typeof(PortfolioManagementClaimNull), new Tuple<HttpStatusCode, object>(HttpStatusCode.Forbidden, new { ErrorCode = PortfoliosErrorCodes.PortfolioManagementClaimNull }) },
                    { typeof(PortfolioIdNullException), new Tuple<HttpStatusCode, object>(HttpStatusCode.BadRequest, new { ErrorCode = PortfoliosErrorCodes.PortfolioNull }) },
                    { typeof(PortfolioUserIdClaimNull), new Tuple<HttpStatusCode, object>(HttpStatusCode.Found, new { ErrorCode = PortfoliosErrorCodes.PortfolioUserIdClaimNull }) },
                    { typeof(PortfolioAccountIdClaimNull), new Tuple<HttpStatusCode, object>(HttpStatusCode.Found, new { ErrorCode = PortfoliosErrorCodes.PortfolioAccountIdClaimNull }) },
                    { typeof(PortfolioGetUnauthorizedException), new Tuple<HttpStatusCode, object>(HttpStatusCode.Found, new { ErrorCode = PortfoliosErrorCodes.PortfolioGetUnauthorizedException }) },
                    { typeof(PortfolioDeleteUnauthorizedException), new Tuple<HttpStatusCode, object>(HttpStatusCode.Forbidden, new { ErrorCode = PortfoliosErrorCodes.PortfolioDeleteUnauthorized }) },
                    { typeof(PortfolioModificationUnauthorizedException), new Tuple<HttpStatusCode, object>(HttpStatusCode.Forbidden, new { ErrorCode = PortfoliosErrorCodes.PortfolioSharedUnauthorized }) },
                    { typeof(PortfolioNameNullException), new Tuple<HttpStatusCode, object>(HttpStatusCode.BadRequest, new { ErrorCode = PortfoliosErrorCodes.PortfolioNameNull }) },
                    { typeof(PortfolioNameAlreadyExists), new Tuple<HttpStatusCode, object>(HttpStatusCode.Found, new { ErrorCode = PortfoliosErrorCodes.PortfolioNameAlreadyExists }) },
                    { typeof(PortfolioEmptyConstituentsList), new Tuple<HttpStatusCode, object>(HttpStatusCode.Found, new { ErrorCode = PortfoliosErrorCodes.PortfolioEmptyConstituentsList }) },
                    { typeof(SubscribeToPortfolioNotAllow), new Tuple<HttpStatusCode, object>(HttpStatusCode.Found, new { ErrorCode = SubscribeToPortfolioErrorCodes.SubscribeToPortfolioNotAllow }) },
                    { typeof(SubscribeToPortfolioClaimNull), new Tuple<HttpStatusCode, object>(HttpStatusCode.Forbidden, new { ErrorCode = PortfoliosErrorCodes.PortfolioManagementClaimNull }) },
                    { typeof(PortfolioNotFoundException), new Tuple<HttpStatusCode, object>(HttpStatusCode.BadRequest, new { ErrorCode = PortfoliosErrorCodes.PortfolioNull }) },
                    { typeof(PortfolioSecurityNotFoundException), new Tuple<HttpStatusCode, object>(HttpStatusCode.BadRequest, null) },
                    { typeof(PortfolioDetailsDeleteUnauthorisedException), new Tuple<HttpStatusCode, object>(HttpStatusCode.Forbidden, new { ErrorCode = 5004 }) },
                    { typeof(PortfolioUploadExcelFormatException), new Tuple<HttpStatusCode, object>(HttpStatusCode.OK, new { ErrorCode = 5203 }) },
                    { typeof(AuthorizationException), new Tuple<HttpStatusCode, object>(HttpStatusCode.Unauthorized,  new { ErrorStatusCode = WebAPIGeneralErrorCodes.AuthorizationFailed, message = "Not authorized. Please attempt re-login." }) },
                };

        public static Tuple<HttpStatusCode, object> Create(Type exceptionType)
        {
            return HandledExceptions.ContainsKey(exceptionType) ? HandledExceptions[exceptionType] : null;
        }
    }
}